import { save, pages, LoadFrom, Delete } from '@/api/activity/activity'
import { pages as candidateSkillList } from '@/api/candidateSkill/candidateSkill'

const state = {
  controllerName: 'activity',
  dataList: {
    listLoading: false,
    list: [],
    shelvesList: [{ 'id': '0', 'name': '校内直招' }, { 'id': ' ', 'name': '' }, { 'id': '2', 'name': '直播' }],
    exerciseList: [{ 'id': 0, 'name': '校内直招' }, { 'id': '', 'name': '' }, { 'id': 2, 'name': '直播' }],
    searchForm: {
      CandidateSkillId: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    imagePercent: false, // 图片上传进度是否显示
    imageUploadPercent: 0, // 图片上传进度
    videoIamgePercent: false, // 视频图片上传进度是否显示
    videoIamgeUploadPercent: 0, // 音频图片上传进度
    candidatelist: []
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`activity/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`activity/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  getcandidateSkillList(context, par) {
    var _state = context.state
    // var parentId = par.parentId || '00000000-0000-0000-0000-000000000000'
    // var level = par.level || 1
    candidateSkillList({
    //   parentId: parentId,
      pageCount: 1000,
      pageIndex: 1
    })
      .then(res => {
        const { Data } = res
        _state.form.candidatelist = Data.List

        // candidatelist.push({ 'candidateId': })
        // if (level === 1) { // 请求一级分类
        //   _state.form.CategoryOneList = Data.List
        // } else if (level === 2) { // 二级分类
        //   _state.form.CategoryTwoList = Data.List
        //   if (par.cls) {
        //     _state.form.model.CategoryTwoId = '00000000-0000-0000-0000-000000000000'
        //     _state.form.model.CategoryThreeId = '00000000-0000-0000-0000-000000000000'
        //     _state.form.CategoryThreeList = []
        //   }
        // } else if (level === 3) { // 二级分类
        //   _state.form.CategoryThreeList = Data.List
        //   if (par.cls) {
        //     _state.form.model.CategoryThreeId = '00000000-0000-0000-0000-000000000000'
        //   }
        // }
      })
      .catch(e => {
        console.log(e)
      })
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    LoadFrom({ id: par })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  save(context, par) {
    var _state = context.state
    if (par.Sort === undefined || par.Sort === '') {
      par.Sort = 0
    }
    par.Sort = parseInt(par.Sort)
    save(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        global.tools.msg('保存成功!', '成功')
        _state.form.formState = false

        context.dispatch('findList')
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.pageIndex
        _state.dataList.searchForm.pageSize = Data.pageSize
        _state.dataList.searchForm.dataCount = Data.count
        _state.dataList.searchForm.pageCount = Data.pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },

  // 上传封面成功
  handleSuccess(context, res, audio) {
    console.log('上传图片res', res)
    var _state = context.state
    _state.form.imagePercent = false
    if (res.Status === 1) {
      if (res.Data.Parameter === 'CoverImg') {
        _state.form.model.CoverImg = res.Data.Url
        _state.form.model.CoverImg = res.Data.Url
      }
    } else {
      global.tools.msg('上传失败，请重新上传！', '错误')
    }
  },
  onProgress(context, file, v) {
    var _state = context.state
    _state.form.imagePercent = true
    _state.form.imageUploadPercent = Math.floor(file.percent) - 1
  },

  // 图片上传处理
  handleVideoIamgeSuccess(context, res) {
    var _state = context.state
    console.log(_state)
    _state.form.videoIamgePercent = false
    _state.form.videoIamgeUploadPercent = 0
    if (res.Status === 1) {
      _state.form.model.CoverImg = res.Data.Url
    } else {
      global.tools.msg('上传失败，请重新上传！', '错误')
    }
  },
  onVideoIamgeProgress(context, file, v) {
    var _state = context.state
    _state.form.videoIamgePercent = true
    _state.form.videoIamgeUploadPercent = Math.floor(file.percent) - 1
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
